using LuckyLang.AST.Expression.ValueTyre;

namespace LuckyLang.AST.Statement;

public class ForStatement : Statement
{
    public SetStatement          Set   { get; set; }
    public Expression.Expression Expr  { get; set; }
    public Statement             State { get; set; }
    public BlockStatement        Block { get; set; }

    public ForStatement(SetStatement set,Expression.Expression expr,Statement state,BlockStatement block)
    {
        Set   = set;
        Expr  = expr;
        State = state;
        Block = block;
    }
    public override void Run(ref VariateManager Manager)
    {
        Manager.Add();
        Set.Run(ref Manager);
        while (true)
        {
            var a = Expr.GetValue(ref Manager);
            if (a is BoolValue)
            {
                if ((bool)a.TheValue)
                {
                    Block.Run(ref Manager);
                    State.Run(ref Manager);
                }
                else
                    break;
            }
            else
                break;
        }
        Manager.Remove();
    }
}